package controllers;




import java.lang.reflect.Constructor;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import controllers.CRUD.ObjectType;

import models.Answer;
import models.Candidate;
import models.Question;
import models.User;
import play.data.binding.Binder;
import play.db.Model;
import play.db.jpa.JPA;
import play.exceptions.TemplateNotFoundException;
import play.i18n.Messages;
import play.mvc.Controller;

public class Questions extends CRUD {
	 public static void create() throws Exception {
	        ObjectType type = ObjectType.get(getControllerClass());
	        notFoundIfNull(type);
	        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
	        constructor.setAccessible(true);
	        Model object = (Model) constructor.newInstance();
	        Binder.bind(object, "object", params.all());
	        validation.valid(object);
	        if (validation.hasErrors()) {
	            renderArgs.put("error", Messages.get("crud.hasErrors"));
	            try {
	                render(request.controller.replace(".", "/") + "/blank.html", type, object);
	            } catch (TemplateNotFoundException e) {
	                render("CRUD/blank.html", type, object);
	            }
	        }
	        object._save();
	        flash.success(Messages.get("crud.created", type.modelName));
	        if (params.get("_save") != null) {
	            redirect("/");
	        }
	        if (params.get("_saveAndAddAnother") != null) {
	            redirect(request.controller + ".blank");
	        }
	        redirect(request.controller + ".show", object._key());
	    }

	public static void blank() throws Exception {
		User user=Security.getConnected();
		if(!user.isAdmin){
			 forbidden();
		}
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Model object = (Model) constructor.newInstance();
        try {
            render(type, object);
        } catch (TemplateNotFoundException e) {
            render("CRUD/blank.html", type, object);
        }
    }
	
	public static void ViewElection(long id){
		Question election=Question.findById(id);
		User user=Security.getConnected();
		boolean candidate=false;
		List<Candidate> candidates=null;
		
		for(Answer a:user.answers){
			if(a.question.id==election.id){
			candidate=true;
			}
			
		}
		if(!candidate){
			 candidates=election.candidates;
		}
		render(user,election,candidates);
	}
	
	
	public static void ViewVote(long id){
		Question election=Question.findById(id);
		User user=Security.getConnected();
	//	Candidate candidate=null;
	
	Query q = JPA.em().
	createQuery("select a from Answer a where voter_id= ?1 AND question_id= ?2")
	.setParameter(1,user.id)
	.setParameter(2, election.id);
	
	Answer answer = (Answer)q.getSingleResult();
//		for(Answer a:user.answers){
//			if(a.candidate.question.id==election.id){
//				 candidate=a.candidate;
//				
//			}
//			
//		}
		render(election,answer);
	}
public static void viewresult(long id){
	Question election=Question.findById(id);
	render(election);
	
}
	public static void End(long id){
		Question election=Question.findById(id);
		election.isEnded=true;
		election.save();
		render(election);
	}
}
